import { ref } from 'vue'
import {
  getGoodSettleDetailById,
  getGoodSettles,
  goodSettleAddOrUpdate,
  goodSettlesDel,
  goodSettlesUpdateStatus,
} from '@/api/order'

export type GoodsForGoodSettle = {
  goodId: string
  id: string
  name: string
  price: number
  specId: string
  payNetworkFee: number
}

export const useForm = () => {
  const form = ref<OrderAPIRequest.GoodSettleForm>({
    delSettlementProgrammeIds: [],
    descr: '',
    id: '',
    moduleIds: [],
    name: '',
    programmeFeeVos: [],
    status: false,
    type: 1,
  })
  const loading = ref(false)

  const resetForm = () => {
    form.value = {
      delSettlementProgrammeIds: [],
      descr: '',
      id: '',
      moduleIds: [],
      name: '',
      programmeFeeVos: [],
      status: false,
      type: 1,
    }
  }

  const save = async () => {
    loading.value = true
    const res = await goodSettleAddOrUpdate(form.value)
    loading.value = false
    return res.data
  }

  return {
    form,
    resetForm,
    save,
  }
}

export const useGetGoodSettles = () => {
  const loading = ref(false)
  const total = ref(0)
  const param = ref({
    goodsKeyword: '',
    goodsOneTypeId: '',
    goodsThreeTypeId: '',
    goodsTwoTypeId: '',
    limit: 50,
    name: '',
    page: 1,
  })
  const list = ref<Array<OrderGoodSettle>>([])

  const getList = async () => {
    loading.value = true
    const res = await getGoodSettles(param.value)
    loading.value = false
    total.value = Number(res.data.data.total)
    list.value = res.data.data.rows
  }

  return {
    list,
    total,
    param,
    loading,
    getList,
  }
}

export const useGoodSettlesEnable = () => {
  const loading = ref(false)

  const enable = async (ids: Array<string>) => {
    loading.value = true
    const res = await goodSettlesUpdateStatus(ids, false)
    loading.value = false
    return res.data
  }

  return {
    loading,
    enable,
  }
}

export const useGoodSettlesDisable = () => {
  const loading = ref(false)

  const disable = async (ids: Array<string>) => {
    loading.value = true
    const res = await goodSettlesUpdateStatus(ids, true)
    loading.value = false
    return res.data
  }

  return {
    loading,
    disable,
  }
}

export const useGoodSettlesDel = () => {
  const loading = ref(false)

  const del = async (ids: Array<string>) => {
    loading.value = true
    const res = await goodSettlesDel(ids)
    loading.value = false
    return res.data
  }

  return {
    loading,
    del,
  }
}

export const useGetGoodSettleDetail = () => {
  const loading = ref(false)
  const detail = ref<OrderGoodSettleDetail>({
    delSettlementProgrammeIds: [],
    descr: '',
    id: '',
    moduleIds: [],
    name: '',
    programmeFeeVos: [],
    status: false,
    type: 1,
  })

  const getDetail = async (id: string) => {
    loading.value = true
    const res = await getGoodSettleDetailById(id)
    loading.value = false
    detail.value = res.data.data.row
    if (!detail.value.delSettlementProgrammeIds) {
      detail.value.delSettlementProgrammeIds = []
    }
  }

  return {
    detail,
    loading,
    getDetail,
  }
}
